home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’95
/
What's On My Mac
/
main.c
< prev
next >
Wrap
Text File
|
1995-06-24
|
12KB
|
478 lines
/*****
*
* What's On My Mac
*
* By Scott T Boyd & Grant Neufeld
*
* Copyright ©1995 by Grant Neufeld & Scott T Boyd
* see full copyright notice in cgi.c
*
* Based in part on "Responder" written by John O'Fallon
*
*****/
#define __MainSegment__
#include <AppleEvents.h>
#include <Menus.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Strings.h>
#include <TextUtils.h>
#include "cgi.h"
#include "DebugUtil.h"
#include "GrabFrontWindow.h"
#include "MemoryUtil.h"
/*** CONSTANT DEFINITIONS ***/
#define NIL 0L
#define APPLE_EVENT_CLASS 'WWWΩ'
#define APPLE_EVENT_ID 'sdoc'
#define MENU_DEF_APPLE 128
#define MENU_DEF_FILE 129
#define ITEM_DEF_QUIT 1
#define BUFFER_SIZE 32768
#define DELIM_STRING "Ω"
#define DELIM_CHAR 'Ω'
#define krHTMLStrs 128
#define krHTMLStrTop 1
#define krHTMLStrImg 2
#define krHTMLStrBottom 3
/*** LOCAL VARIABLES ***/
char vImageFileName[32];
long vImageFileNameSize;
Str255 vHTMLStrTop;
long vHTMLStrTopSize;
Str255 vHTMLStrImg;
long vHTMLStrImgSize;
Str255 vHTMLStrBottom;
long vHTMLStrBottomSize;
/*** FUNCTION PROTOTYPES ***/
void HandleKeyDown ( int, int );
void HandleMouseDown ( Point );
void HandleMenuChoice ( long );
void UpdateScreen ( void );
void AboutBox ( void );
pascal OSErr HandOpenApp ( AppleEvent *, AppleEvent *, long );
pascal OSErr HandOpenDoc ( AppleEvent *, AppleEvent *, long );
pascal OSErr HandPrintDoc ( AppleEvent *, AppleEvent *, long );
pascal OSErr HandQuitApp ( AppleEvent *, AppleEvent *, long );
pascal OSErr HandClientRequest ( AppleEvent *, AppleEvent *, long );
void StatusSetUp ( void );
void InstallAllEvents ( void );
void MyCGIInit ( void );
void MyCGIProcess ( CGIHdl );
/*** GLOBAL VARIABLES ***/
int QuitApp; // Boolean for "Should we quit"?
MenuHandle MenuApple; // Handle to the Apple Menu (for finding DA names)
WindowPtr MyWindow; // Handle to our window
WindowRecord MyWindArea; // A window record for the status screen
PicHandle MyPic; // A handle to the window background PICT
/* MAIN sets up the environment and executes the event loop. */
void
main ( void )
{
OSErr MyErr;
Handle MenuBar;
MenuHandle MenuFile;
EventRecord Action;
/* Initialize our 'quit' global */
QuitApp = FALSE;
/* Now do all the right Mac things (See Inside Mac) */
InitGraf ( &qd.thePort );
InitFonts ();
InitWindows ();
InitMenus ();
FlushEvents ( everyEvent, nil );
TEInit ();
InitDialogs ( NIL );
InitCursor ();
MaxApplZone ();
/* Menu Bar set-up, see Inside Mac for details */
MenuBar = GetNewMBar ( MENU_DEF_APPLE );
SetMenuBar ( MenuBar );
MenuApple = GetMenuHandle ( MENU_DEF_APPLE );
MenuFile = GetMenu ( MENU_DEF_FILE );
AppendResMenu ( MenuApple, 'DRVR' );
DrawMenuBar ();
// Let the Event Manager know we want AppleEvents
InstallAllEvents ();
InitCGIUtil ();
MyCGIInit ();
StatusSetUp ();
/* Go through the event loop as long as we aren't supposed to quit */
while ( QuitApp == FALSE )
{
WaitNextEvent ( everyEvent, &Action, 0L, 0L ); // Ask the OS for an event
switch (Action.what)
{
case mouseDown: // They clicked the mouse...
HandleMouseDown ( Action.where );
break;
case keyDown: // They pressed a key...
HandleKeyDown ( (int)Action.message,(int)Action.modifiers );
break;
case updateEvt: // Our window needs to be re-painted...
if ( (WindowPtr)Action.message == MyWindow )
{
BeginUpdate ( MyWindow );
UpdateScreen ();
EndUpdate ( MyWindow );
}
break;
case kHighLevelEvent: // We have an AppleEvent... so let the OS...
MyErr = AEProcessAppleEvent ( &Action ); // dispatch it (see Inside Mac).
if ( MyErr != noErr )
{
SysBeep (2); // Well behaved apps would do more than beep!
}
break;
}
}
}
/****************************************************************/
/* When a key is pressed, this routine processes it. */
/****************************************************************/
void
HandleKeyDown (int KeyPress, int Modifiers)
{
char Key;
int Modifier;
long int MenuChoice;
Key = KeyPress & charCodeMask;
Modifier = Modifiers & cmdKey;
if (Modifier > 0)
{ // We pay attention only to Command keys.
MenuChoice = MenuKey (Key); // If the user hit a shortcut key,
HandleMenuChoice (MenuChoice); // see if there's a menu option to execute.
}
}
/****************************************************************/
/* When the mouse button gets pressed, this routine handles it. */
/****************************************************************/
void HandleMouseDown (Point Where)
{
WindowPtr WhichWind;
short int WhichPart;
long int MenuChoice;
Rect DragRect; // Used to define dragable area for our window
WhichPart = FindWindow (Where, &WhichWind); // Find out where they clicked
switch (WhichPart)
{
case inMenuBar: // clicking in the menu bar is interesting...
MenuChoice = MenuSelect (Where); // tell the OS to handle it
HandleMenuChoice (MenuChoice); // and process any option they may have chosen
break;
case inDrag:
DragRect.top = -32767; // set-up the drag area
DragRect.bottom = 32767;
DragRect.left = -32767;
DragRect.right = 32767;
DragWindow ( WhichWind, Where, &DragRect ); // let the OS do the dragging
break;
}
}
/****************************************************************/
/* When the user selects a menu option, either by mouse or */
/* shortcut key, this routine handles it. */
/****************************************************************/
void HandleMenuChoice (long MenuChoice)
{
int TheMenu;
int TheItem;
Str255 DAName;
int DANumber;
if (MenuChoice != 0)
{ // Ignore NULL selections
TheMenu = HiWord (MenuChoice); // find the menu number
TheItem = LoWord (MenuChoice); // find the item number
switch (TheMenu)
{
case MENU_DEF_APPLE:
if (TheItem == 1)
{
AboutBox ();
}
else
{
GetMenuItemText (MenuApple,TheItem,DAName);
DANumber = OpenDeskAcc (DAName);
}
break;
case MENU_DEF_FILE:
switch (TheItem)
{
case ITEM_DEF_QUIT:
QuitApp=TRUE;
break;
}
break;
}
HiliteMenu (0);
}
} /* */
/****************************************************************/
/* STATUSSETUP creates our status window and paints the */
/* background. */
/****************************************************************/
void StatusSetUp ()
{
Rect WindowRect;
Rect PicRect;
WindowRect.top =40;
WindowRect.bottom =467;
WindowRect.left =20;
WindowRect.right =735;
MyWindow = NewWindow ( &MyWindArea, &WindowRect, "\pWhat's On My Mac", TRUE, (int)0,
(WindowPtr)(-1), FALSE, (long)5 );
// Open and Show a status window
ShowWindow (MyWindow);
SetPort (MyWindow);
MyPic = GetPicture (128); // Load the background of the window from a PICT resource
if ( MyPic != 0 )
{
// Make sure we got it
PicRect = (*MyPic)->picFrame; // size the rect
DrawPicture (MyPic, &PicRect); // and display it
}
}
/****************************************************************/
/* UPDATESCREEN updates each field on the status screen. */
/****************************************************************/
void
UpdateScreen ()
{
Rect PicRect;
if ( MyPic != nil )
{
// As long as we still have a hold of the PICT
PicRect = (*MyPic)->picFrame; // get it's rect
DrawPicture ( MyPic, &PicRect ); // and display it
}
}
/******************************************************/
/* ABOUTBOX - Displays a window with the about */
/* box as a PICT resource, this proc */
/* automatically displays a centered window with */
/* the PICT in it and waits for the user to */
/* click the mouse button. */
/******************************************************/
void
AboutBox ( void )
{
SysBeep ( 2 );
}
/* */
void
MyCGIInit ( void )
{
Handle theResource;
/* init html strings */
GetIndString ( vHTMLStrTop, krHTMLStrs, krHTMLStrTop );
P2CStr ( vHTMLStrTop );
vHTMLStrTopSize = strlen ( (char *)vHTMLStrTop );
GetIndString ( vHTMLStrImg, krHTMLStrs, krHTMLStrImg );
P2CStr ( vHTMLStrImg );
vHTMLStrImgSize = strlen ( (char *)vHTMLStrImg );
GetIndString ( vHTMLStrBottom, krHTMLStrs, krHTMLStrBottom );
P2CStr ( vHTMLStrBottom );
vHTMLStrBottomSize = strlen ( (char *)vHTMLStrBottom );
/* init file name string */
theResource = Get1Resource ( 'CSTR', 128 );
my_assert ( strlen((char*)(*theResource)) < 32, "MyCGIInit: theResource is too big" );
HLock ( theResource );
strcpy ( vImageFileName, (char*)(*theResource) );
HUnlock ( theResource );
ReleaseResource ( theResource );
vImageFileNameSize = strlen ( vImageFileName );
} /* MyCGIInit */
/* */
void
MyCGIProcess ( CGIHdl theCGIHandle )
{
long totalSize;
unsigned short imgwidth;
unsigned short imgheight;
unsigned short clickH;
unsigned short clickV;
char * offset;
totalSize = gHTTPHeaderOKSize + vImageFileNameSize + vHTMLStrTopSize
+ vHTMLStrImgSize + 26 + vHTMLStrBottomSize;
HLock ( (Handle)theCGIHandle );
(*theCGIHandle)->responseData = (char *) MyNewPtr ( totalSize, nil );
if ( (*theCGIHandle)->responseData != nil )
{
offset = strchr ( (*theCGIHandle)->responseData, ',' );
offset[0] = nil;
clickH = atoi ( (*theCGIHandle)->http_search_args );
clickV = atoi ( offset + 1 );
HUnlock ( (Handle)theCGIHandle );
ClickOnTheFrontWindow ( clickH, clickV );
}
GrabFrontWindowAsJPEG ( &imgwidth, &imgheight );
if ( (*theCGIHandle)->responseData != nil )
{
(*theCGIHandle)->responseSize = totalSize;
/* generate an HTML string and send it back */
sprintf ( (*theCGIHandle)->responseData,
"%s%s%s%s WIDTH=%d HEIGHT=%d%s",
gHTTPHeaderOK, vHTMLStrTop, vImageFileName, vHTMLStrImg,
imgwidth, imgheight, vHTMLStrBottom );
SnapshotSound ();
}
} /* MyCGIProcess */
/* registers the event handlers with the Event Manager. */
#pragma segment AppleEvent
void
InstallAllEvents ( void )
{
OSErr theErr;
AEEventHandlerUPP MyUPP;
/* get the Universal Proc Pointer (required for PPC calls) */
MyUPP = NewAEEventHandlerProc(HandOpenApp);
/* install the handler */
theErr = AEInstallEventHandler (kCoreEventClass,kAEOpenApplication,MyUPP,0,FALSE);
MyUPP = NewAEEventHandlerProc(HandOpenDoc);
theErr = AEInstallEventHandler (kCoreEventClass,kAEOpenDocuments,MyUPP,0,FALSE);
MyUPP = NewAEEventHandlerProc(HandPrintDoc);
theErr = AEInstallEventHandler (kCoreEventClass,kAEPrintDocuments,MyUPP,0,FALSE);
MyUPP = NewAEEventHandlerProc(HandQuitApp);
theErr = AEInstallEventHandler (kCoreEventClass,kAEQuitApplication,MyUPP,0,FALSE);
MyUPP = NewAEEventHandlerProc ( CGIAEHandle );
theErr = AEInstallEventHandler (APPLE_EVENT_CLASS,APPLE_EVENT_ID,MyUPP,0,FALSE);
if ( theErr != noErr )
{
// Well behaved apps would do more than beep!
SysBeep (2);
}
} /* InstallAllEvents */
/**************************************************************/
/* APPLE EVENT ROUTINES: Handlers, Installers, and Processors */
/**************************************************************/
#pragma segment AppleEvent
pascal OSErr HandOpenApp (AppleEvent *TheRequest, AppleEvent *TheReply, long Reference)
{
UpdateScreen ();
return (noErr);
}
#pragma segment AppleEvent
pascal OSErr HandOpenDoc (AppleEvent *TheRequest, AppleEvent *TheReply, long Reference)
{
UpdateScreen ();
return (noErr);
}
#pragma segment AppleEvent
pascal OSErr HandPrintDoc (AppleEvent *TheRequest, AppleEvent *TheReply, long Reference)
{
UpdateScreen ();
return (noErr);
}
#pragma segment AppleEvent
pascal OSErr HandQuitApp (AppleEvent *TheRequest, AppleEvent *TheReply, long Reference)
{
UpdateScreen ();
QuitApp = TRUE;
return (noErr);
}
#pragma segment Main